﻿using System;
using Sensors;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace NavSensorTest
{
    public partial class NavSensorTestForm : Form
    {
        public NavSensorTestForm()
        {
            InitializeComponent();
            mySensor = new HTCNavSensor(this);
            mySensor.Rotated += new NavSensorMoveHandler(mySensor_Rotated);
            Disposed += new EventHandler(NavSensorTestForm_Disposed);
        }

        void mySensor_Rotated(double rotationsPerSecond, double radialDelta)
        {
            Debug.WriteLine(myCurrentRadians.ToString());
            myCurrentRadians += (radialDelta * Math.PI * 2);

            myRPS.Text = string.Format("RPS: {0}", rotationsPerSecond.ToString());
            myRadialDelta.Text = string.Format("Radial Delta: {0}", radialDelta.ToString());

            Invalidate();
        }

        void NavSensorTestForm_Disposed(object sender, EventArgs e)
        {
            if (mySensor != null)
            {
                mySensor.Dispose();
                mySensor = null;
            }
        }

        double myCurrentRadians = 0;
        protected override void OnPaint(PaintEventArgs e)
        {
            // lets draw a line that radiates from the center of the screen
            // the rate it rotates will be that of the nav sensor
            int centerX = ClientSize.Width / 2;
            int centerY = ClientSize.Height / 2;

            int length = Math.Min(ClientSize.Width, ClientSize.Height) - 20;

            int height = (int)(Math.Sin(myCurrentRadians) * length);
            int width = (int)(Math.Cos(myCurrentRadians) * length);

            e.Graphics.DrawLine(new Pen(Color.Red), centerX, centerY, centerX + width, centerY + height);
        }

        HTCNavSensor mySensor;

        private void myExitMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}